﻿var Admin = {};
var Login = {};
var Index = {};
var Pager = {};
var CheckBox = {};

/* -----  Admin ----- */

Admin.Loaded = false;
Admin.Init = function () {
    // 其它初始化
    $('a').live('focus', function () { $(this).blur(); });
    $('input[type=radio]').live('focus', function () { $(this).blur(); });
    $('input[type=checkbox]').live('focus', function () { $(this).blur(); });
    $('input[type=checkbox]').css('border', 'none');
    $('.btn-middle').css({ 'margin-bottom': (J.IsIE6 ? 1 : 3) + 'px' }); //修改按钮水平对齐
    // 隔行变色
    $('table.data-table tr:even').addClass('even');

    // 三态按钮
    $('.btn')
        .live('mousedown', function () { $(this).addClass('btn-active'); })
        .live('mouseup', function () { $(this).removeClass('btn-active'); })
        .live('mouseover', function () { $(this).addClass('btn-hover'); })
        .live('mouseout', function () { $(this).removeClass('btn-active'); $(this).removeClass('btn-hover'); });
    $('.btn-lit')
        .live('mousedown', function () { $(this).addClass('btn-lit-active'); })
        .live('mouseup', function () { $(this).removeClass('btn-lit-active'); })
        .live('mouseover', function () { $(this).addClass('btn-lit-hover'); })
        .live('mouseout', function () { $(this).removeClass('btn-lit-active'); $(this).removeClass('btn-lit-hover'); });

    // 浏览改大小
   
    $(window).bind('resize', function () {
        /*
        if ((Admin.Loaded || $.browser.mozilla) && (Admin.IsLoginPage || Admin.IsIndexPage)) {
        var url = window.location.href;
        var pos = url.indexOf('#'); // 有#时不起作用
        window.location = pos > -1 ? url.substring(0, pos) : url;
        }
        Admin.Loaded = true;
        */
        Index.InitWinsize();
    });
};

Admin.Logout = function () {
    jBox.confirm('确定要退出系统吗？', '提示', function (v, h, f) {
        if (v == 'ok') {             
            location.href = Admin.LogoutUrl;
        }
        return true;
    });
};

Admin.SubmitForm = function (name) {
    var form = (name == undefined || name == '') ? document.forms[0] : document.forms[name];
    if (J.IsObject(form)) {
        $('.btn-lit').setDisabled(true).click(function () { return false; });
        if (jBox != undefined) {
            jBox.tip('正在提交...', 'loading');
        }
        form.submit();
    }
};

/* -----  Login ----- */

Login.Init = function () {
    // IE情况下修正自适应高度
    if ($.browser.msie) {
        var height = $(document).height() - 338;
        if (J.IsIE6) {
            height = height - 20;
        }
        $('#login_auto_height').css({ height: height - ($('#login_form tr').length - 2) * 38 - 53 + 30 });
    }
    // 提交登录
    $('#login_btn').click(Login.Submit);
    $('#password').keydown(function (event) { J.EnterSubmit(event, Login.Submit); });    
    $('#username').keydown(function (event) { J.EnterSubmit(event, Login.Submit); }).focus();
};



Login.Tip = function (text, icon) {
    jBox.tip(text, icon, { top: '100px' });

};

Login.Submit = function () {
    var username = $('#username').trim();
    var password = $('#password').trim();

    var cbxPass = $("#cbxPass").is(':checked');     
   
    if (username == '') {
        Login.Tip('请输入用户！', 'warning');
        $('#username').focus();
        return;
    }
    if (password == '') {
        Login.Tip('请输入密码！', 'warning');
        $('#password').focus();
        return;
    }
    var data = { username: username, password: password, cbxPass: cbxPass };

    //直接到首页
    Login.Tip('正在登录，请稍后...', 'loading');
    J.PostJson(Admin.LoginUrl, data, function (data) {
        if (data == 1) {
            top.location.href = Admin.IndexUrl;
        } else {
            Login.Tip(data.msg, 'error');
            $('#username').select();
        }
    }, function () {
        Login.Tip('登录出错！', 'error');
    });
};

/* -----  Index ----- */
Index.MenuIndex = 0;
Index.MenuSpeed = 250;
Index.InitWinsize = function () {
    // 初始化高与宽
    var win = $(window);
    var width = win.width() - 182 - 0 - 2;
    var height = win.height() - 64 - 2 - 33;
    var height_c = height - 29 - 8 - 7;
    $('#left').height(height);
    $('#right').height(height).width(width);
    $('#menu-container').height(height - 3);
    $('#content-container').height(height_c);
    $('#loading').css('padding-top', height_c / 2);
}
Index.Init = function () {
    // 初始化高与宽
    Index.InitWinsize();

    // 菜单切换效果
    var tits = $('.menu-tit');
    var lists = $('.menu-list').hide();
    $(lists[0]).slideDown(Index.MenuSpeed);
    $.each(tits, function (i, el) { $(el).attr('index', i); });
    tits.click(function (e) {
        var me = $(this);
        var index = parseInt(me.attr('index'));
        if (index != Index.MenuIndex) {
            var last = Index.MenuIndex;
            Index.MenuIndex = index;
            $(lists[last]).slideUp(Index.MenuSpeed);
            $(lists[index]).slideDown(Index.MenuSpeed);
        }
    });

    // 菜单点击事件
    var links = $('a[target=content]');
    if (links.length > 0) {
        links.bind('click', function (e) {
            if (e.preventDefault)
                e.preventDefault();
            else
                e.returnValue = false;

            Index.Open($(this).attr('href'));
        });
        //links[0].click();
    }
    Index.Open(Admin.IndexStartPage);
};

Index.OutputIframe = function () {
    var scrolling = $.isIE6 == true ? 'yes' : 'auto';
    document.write('<iframe id="content" width="100%" height="100%" class="hide" marginwidth="0" marginheight="0" frameborder="0" scrolling="' + scrolling + '" onload="$(\'#loading\').hide();$(this).show();" src=""></iframe>');
};


Index.Login = function CloseWin() {

    var strFullPath = window.document.location.href;
    var strPath = window.document.location.pathname;
    var pos = strFullPath.indexOf(strPath);
    var prePath = strFullPath.substring(0, pos);
    var postPath = strPath.substring(0, strPath.substr(1).indexOf('/') + 1);
    var login = prePath + postPath + "/Login.aspx";

    window.open(login, '_top');
} 


Index.Open = function (url) {
    if (url != '') {
        top.$('#loading').show();
        if (url.indexOf('#') == -1) {
            url = url + (url.indexOf('?') == -1 ? '?___t=' : '&___t=') + Math.random();
        } else {
            var arr = url.split('#');
            url = arr[0] + (arr[0].indexOf('?') == -1 ? '?___t=' : '&___t=') + Math.random() + '#' + arr[1];
        }
        top.$('#content').hide().attr('src', url);

        // 可能在jBox里点击跳转，同时关闭它
        if (jBox) {
            jBox.close();
        }
    }
};

Index.SetTitle = function (title) {
    top.$('#index-title').html(title);
};

Pager.Data = {};
Pager.Output = function (urlFormat, pageSize, pageIndex, pageCount, recordCount) {
    pageSize = parseInt(pageSize, 10);
    pageIndex = parseInt(pageIndex, 10);
    pageCount = parseInt(pageCount, 10);
    recordCount = parseInt(recordCount, 10);

//    if (pageIndex < 1)
//        pageIndex = 1;
//    if (pageIndex > pageCount)
//        pageIndex = pageCount;

    Pager.Data.urlFormat = urlFormat;
    Pager.Data.pageCount = pageCount;

    function _getLink(text, enabled, urlFormat, index) {
        if (enabled == false)
            return J.FormatString(' <a class="button-white" style="filter:Alpha(Opacity=60);opacity:0.6;" href="javascript:void(0);"><span>{0}</span></a>', text);
        else
            return J.FormatString(' <a class="button-white" href="javascript:Index.Open(\'' + urlFormat + '\');"><span>{1}</span></a>', index, text);
    }

    var html = [];
    html.push('<div class="pager-bar">');
    html.push(J.FormatString('<div class="msg">共{0}条记录，当前第{1}/{2}，每页{3}条记录</div>', recordCount, pageIndex + 1, pageCount, pageSize));
    html.push(_getLink('首页', pageIndex > 0, urlFormat, 0));
    html.push(_getLink('上一页', pageIndex > 0, urlFormat, pageIndex - 1));
    html.push(_getLink('下一页', pageCount > 0 && pageIndex < pageCount-1, urlFormat, pageIndex + 1));
    html.push(_getLink('尾页', pageCount > 0 && pageIndex < pageCount-1, urlFormat, pageCount));
    html.push('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
    html.push('第<input id="current-index" onkeydown="J.EnterSubmit(event, Pager.Jump);" class="input-small" style="text-align:center;" type="text" value="' + (pageIndex > -1 ? pageIndex+1 : '') + '" />页');
    html.push('&nbsp;&nbsp;&nbsp;&nbsp;');
    html.push('<a class="button-white"' + (pageCount <= 1 ? ' style="filter:Alpha(Opacity=60);opacity:0.6;" href="javascript:void(0);"' : ' href="javascript:Pager.Jump();"') + '><span>跳转</span></a>');
    html.push('</div>');

    document.write(html.join(''));
};
Pager.Jump = function () {
    var index = $('#current-index').trim();
    if (J.IsIntPositive(index) == false || parseInt(index) < 1 || parseInt(index) > Pager.Data.pageCount) {
        $('#current-index').val('').focus();
        return;
    }
    Index.Open(J.FormatString(Pager.Data.urlFormat, index-1));
}

CheckBox.CheckAll = function (checkBox, containerId) {
    if (containerId == undefined)
        $("input[type=checkbox]").each(function () { this.checked = checkBox.checked; });
    else {
        $("#" + containerId + " input[type=checkbox]").each(function () { this.checked = checkBox.checked; });
    }
};
CheckBox.GetCheckedIds = function (containerId) {
    if (containerId == undefined)
        return $("input.check-box:checked").map(function () { return $(this).attr("rel"); }).get();
    else
        return $("#" + containerId + " input.check-box:checked").map(function () { return $(this).attr("rel"); }).get();
};

// 初始化
$(function () {
    Admin.Init();
    if (Admin.IsLoginPage) {
        Login.Init();
    }
    if (Admin.IsIndexPage) {
        Index.Init();
    }
});

// 只调用最顶的jBox
if (top.jBox != undefined) {
    window.jBox = top.jBox;
}


 function add_SkillClass() {
    var html1 = '<div class="msg-div">' +
                        '<div class="field" style="text-align:center;">分类名称：<input maxlength="100" style="width:60%;" type="text" id="amount" name="amount" />&nbsp;&nbsp;<font style="color:red">*</font>' +
                        '</div></div>';
    var data = {};
    var states = {};
    
    states.state2 = {
        content: html1,
        buttons: { '提交': 1, '取消': 0 },
        buttonsFocus: 1, // focus on the second button
        submit: function (v, o, f) {
            if (v == 0) {
                return true; // close the window
            }
            else {
                data.amount = f.amount; //获取技能名称值
                if (data.amount == '' || $.trim(data.amount) == '') {
                    $("#amount").css("border", "1px solid #A40000");
                    $('#amount').focus();
                    return false;
                } else {
                    
                    $.ajax({
                        type: "Post",
                        url: "Skill.aspx/AddSkillClass",
                        data: "{'name':'" + toJsString(data.amount) + "'}",
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (data) {
                            if (data.d == '1') {

                                location.href = location.href;
                                $.jBox.close(); //关闭当前窗口；
                                jBox.tip("保存成功!", "");

                            } else if (data.d == '-2') {
                                jBox.tip("该分类信息已经存在...", "error");
                             } else {
                                $.jBox.nextState('<div class="msg-div">保存失败...</div>');
                            }
                        },
                        error: function (err) {
                            alert(err);
                        }
                    });
                    return false;
                }

            }

            return false;
        }
    };
    states.state3 = {
        content: '',
        buttons: {} // no buttons
    };
    states.state4 = {
        content: '',
        buttons: { '确定': 0 }
    };

    $.jBox.open(states, '提交', 450, 'auto');

    $("#amount").focus();
}

function add_Skill(SkillClass_ID)
{
    var html1 = '<div class="msg-div">' +
                        '<div class="field" style="text-align:center;">技能名称：<textarea maxlength="100" style="width:50%;"  id="skill" name="skill"></textarea>&nbsp;&nbsp;<font style="color:red">*</font>' +
                        '</div></div>';
    $("#skill").focus();
    var data = {};
    var states = {};

    states.state2 = {
        content: html1,
        buttons: { '提交': 1, '取消': 0 },
        buttonsFocus: 1, // focus on the second button
        submit: function (v, o, f) {

            if (v == 0) {
                return true; // close the window
            }
            else {
                data.skill = f.skill; //获取技能名称值
                if (data.skill == '' || $.trim(data.skill) == '') {
                    $("#skill").attr("style", "border: 1px solid #A40000;");
                    $('#skill').focus();
                    return false;
                } else {
                    $.ajax({
                        type: "Post",
                        url: "Skill.aspx/AddSkill",
                        data: "{'id':'" + SkillClass_ID + "','name':'" + toJsString(data.skill) + "'}",
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (data) {
                            if (data.d == '1') {

                                location.href = location.href;
                                $.jBox.close(); //关闭当前窗口；
                                jBox.tip("保存成功!", "");

                            } else if (data.d == '-2') {
                                jBox.tip("该技能信息已经存在...", "error");
                            } else {
                                $.jBox.nextState('<div class="msg-div">保存失败...</div>');
                            }
                        },
                        error: function (err) {
                            alert(err);
                        }
                    });
                    return false;
                }

            }

            return false;
        }
    };
    states.state3 = {
        content: '',
        buttons: {} // no buttons
    };
    states.state4 = {
        content: '',
        buttons: { '确定': 0 }
    };

    $.jBox.open(states, '提交', 600, 'auto');
    $('#skill').focus();
}


function Edit_SkillClass(SkillClass_ID, SkillClass_Name) {
    var html1 = '<div class="msg-div">' +
                        '<div class="field" style="text-align:center;">分类名称：<input maxlength="100" style="width:60%;"  type="text" id="amount1" name="amount1" value=' + toJsHtmlString(SkillClass_Name) +
                        ' />&nbsp;&nbsp;<font style="color:red">*</font>' +
                        '</div></div>';
    
    var data = {};
    var states = {};

    states.state2 = {
        content: html1,
        buttons: { '确定': 1, '取消': 0 },
        buttonsFocus: 1, // focus on the second button
        submit: function (v, o, f) {

            if (v == 0) {
                return true; // close the window
            }
            else {
                data.amount1 = f.amount1; //获取技能名称值

                if (data.amount1 == '' || $.trim(data.amount1) == '') {
                    $("#amount1").attr("style", "border: 1px solid #A40000;"); 
                    $('#amount1').focus();
                    return false;
                } else {
                    $.ajax({
                        type: "Post",
                        url: "Skill.aspx/EditSkillClass",
                        data: "{'SkillClass_ID':'" + SkillClass_ID + "','SkillClass_Name':'" + toJsString(data.amount1) + "'}",
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (data) {
                            if (data.d == '1') {
                                location.href = location.href;
                                $.jBox.close(); //关闭当前窗口；
                                jBox.tip("修改成功!", "");
                            } else if (data.d == '-2') {
                                jBox.tip("该分类信息已经存在!", "error");
                            } else {
                                $.jBox.nextState('<div class="msg-div">修改失败...</div>');
                            }
                        },
                        error: function (err) {
                            alert(err);
                        }
                    });
                    return false;
                }

            }

            return false;
        }
    };
    states.state3 = {
        content: '',
        buttons: {} // no buttons
    };
    states.state4 = {
        content: '',
        buttons: { '确定': 0 }
    };

    $.jBox.open(states, '提交', 450, 'auto');
    $('#amount1').focus();
}

function Edit_Skill(Skill_ID, Skill_Name,FK_SkillClass_ID) {
    var html1 = '<div class="msg-div">' +
                        '<div class="field" style="text-align:center;">技能名称：<textarea maxlength="100" style="width:50%;" id="skill" name="skill" >' + Skill_Name +
                        '</textarea>&nbsp;&nbsp;<font style="color:red">*</font>' +
                        '</div></div>';
    var data = {};
    var states = {};

    states.state2 = {
        content: html1,
        buttons: { '确定': 1, '取消': 0 },
        buttonsFocus: 1, // focus on the second button
        submit: function (v, o, f) {

            if (v == 0) {
                return true; // close the window
            }
            else {
                data.skill = f.skill; //获取技能名称值

                if (data.skill == '' || $.trim(data.skill) == '') {
                    $("#skill").attr("style", "border: 1px solid #A40000;");
                    $('#skill').focus();
                    return false;
                } else {
                    $.ajax({
                        type: "Post",
                        url: "Skill.aspx/EditSkill",
                        data: "{'Skill_ID':'" + Skill_ID + "','Skill_Name':'" + toJsString(data.skill) + "','FK_SkillClass_ID':'" + FK_SkillClass_ID + "'}",
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (data) {
                            if (data.d == '1') {
                                location.href = location.href;
                                $.jBox.close(); //关闭当前窗口；
                                jBox.tip("修改成功!", "");
                            } else if (data.d == '-2') {
                                jBox.tip("该技能信息已经存在!", "error");
                            } else {
                                $.jBox.nextState('<div class="msg-div">修改失败...</div>');
                            }
                        },
                        error: function (err) {
                            alert(err);
                        }
                    });
                    return false;
                }

            }

            return false;
        }
    };
    states.state3 = {
        content: '',
        buttons: {} // no buttons
    };
    states.state4 = {
        content: '',
        buttons: { '确定': 0 }
    };
    $.jBox.open(states, '提交', 600, 'auto');
    $('#skill').focus();
}

function delSkillClass() {

    //循环删除分类
    var fl = "";
    $(".container").find("input[id^=cbf]").each(function () {
        if ($(this).is(":checked")) {
            var id = this.id.replace("cbf", "");
            fl += id + ",";
        }
    })


    //循环技能id
    var jn = "";
    $(".container").find("input[id^=cbz]").each(function () {
        if ($(this).is(":checked")) {
            var zid = this.id.replace("cbz", "");
            jn += zid+",";
        }
    })

    if(fl == '' && jn == ''){
        alert("您没有选择任何项，请选择！！");
        return;
    } else {


                $.ajax({
                    type: "Post",
                    url: "Skill.aspx/DelSkill",
                    data: "{'Skill_ID':'" + jn.substring(0, jn.length - 1) + "','id':'" + fl.substring(0, fl.length - 1) + "'}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (data) {
                        if (data.d == '1') {
                            location.href = location.href;
                            $.jBox.close(); //关闭当前窗口；
                            jBox.tip("删除成功!", "");
                        } else if (data.d == '-2') {
                            location.href = location.href;
                            jBox.tip("无法删除，该技能信息已被使用!", "error");
                        } else {
                            location.href = location.href;
                            jBox.tip("删除失败...", "error");
                        }
                    },
                    error: function (err) {
                        alert(err);
                    }
                });
    
    }
}

var _current_boxid;
CloseCurrentBox = function () {
    $.jBox.close(_current_boxid);
}
CloseBoxTip = function () {
    $.jBox.closeTip();
}

function toJsString(name) {
    return name.replace(/\"/g, '\"').replace(/\'/g, "\\'").replace(/\n/g, '');
 }

 function toJsHtmlString(name) {
     return name.replace('\'', '&#39').replace('\"', '&quot;').replace(/\n/g, '');
  }